const
	LEFT = 1;
	RIGHT = 2;
	BEYOND = 3;
	BEHIND = 4;
	BETWEEN = 5;
	ORIGIN = 6;
	DESTINATION = 7;
	TOUCHING = 1;
	CROSSING = 2;
	INESSENTIAL = 3;

function isPtInPol(a : pt; var mas : ap; n : longint) : longint;   // 0 = outside, 1 = inside, 2 = border

	function dist(p : pt) : extended;
	begin
		result := p.x * p.x + p.y * p.y;
	end;

	function cmp(a, b : pt) : boolean;
	begin
		result := (a.x = b.x) and (a.y = b.y);
	end;

	function PlaceEnum (p0, p1, c : pt) : longint;

	var 
		sa : extended;
		a, b : pt;
		
  begin
		a.x := p1.x - p0.x;
		a.y := p1.y - p0.y;
		b.x := c.x - p0.x;
		b.y := c.y - p0.y;
		sa := a.x * b.y - b.x * a.y;
		if sa > 0 then result := LEFT else
		if sa < 0 then result := RIGHT else
		if (a.x * b.x < 0) or (a.y * b.y < 0) then result := BEHIND else
		if dist(a) < dist(b) then	result := BEYOND else
		if cmp(p0, c) then result := ORIGIN else
		if cmp(p0, c) then  result := DESTINATION else result := BETWEEN;
  end;

	function edgeType(p0, p1, c : pt) : longint;
	
	var 
		p : int64;
		
  begin
		p := PlaceEnum(p0, p1, c);
		if p = LEFT then 
			if (p0.y < c.y) and (c.y <= p1.y) then result := CROSSING else result := INESSENTIAL else
		if p = RIGHT then 
			if (p1.y < c.y) and (c.y <= p0.y) then result := CROSSING else result := INESSENTIAL else	
		if (p = BETWEEN) or (p = ORIGIN) or (p = DESTINATION) then result := TOUCHING else result := INESSENTIAL;
	end;
	
var 
	i, k, i1, i2, cur : longint;

begin
	k := 0;
  for i := 1 to n do begin
		i1 := i; 
		i2 := i + 1; 
		if i = n then i2 := 1;
		cur := edgeType(mas[i1], mas[i2], a);
		if cur = TOUCHING then begin
			result := 2;
			exit;
		end;
		if cur = CROSSING then k := 1 - k;
	end;
	result := k;
end;